﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Facebook;
using Newtonsoft.Json.Linq;

namespace Hangout.Web.Core.Tokens
{
    public class FacebookTokens
    {
        public static void RefreshTokens(int userId,ref Model.HangoutDBEntities context)
        {
            Model.User user = Core.Accounts.User.GetUser((int)userId, ref context);
            if (user != null)
            {
                if (user.AccessToken != null)
                {
                    FacebookClient client = new FacebookClient(user.AccessToken);

                    string result = client.Get(@"/me/likes/").ToString();

                    JObject obj = JObject.Parse(result);

                    List<JToken> token = obj.SelectToken("data").ToList();

                    foreach (JToken t in token)
                    {
                       Model.Token x= Core.Tokens.Tokens.InsertTokenIfNotExists(t.SelectToken("name").ToString().ToLower(), ref context);
                       Core.Tokens.Tokens.AddUserToken(userId, x.TokenId, ref context);
                    }
                }
            }
        }
    }
}